package bch.elasticsearch.config;

import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import com.nlf.calendar.util.HolidayUtil;
import org.lionsoul.ip2region.xdb.Searcher;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.Path;
import java.io.File;
import java.util.Date;

public class Ip2RegionDemo {

    public static void main(String[] args) throws Exception {
        // 通过类路径读取 resources 下的文件
        InputStream is = Ip2RegionDemo.class.getClassLoader().getResourceAsStream("ip2region.xdb");
        if (is == null) {
            throw new RuntimeException("ip2region.xdb not found in resources!");
        }

        // 拷贝到临时文件（Searcher 只能接收文件路径或 byte[]）
        File tempFile = File.createTempFile("ip2region", ".xdb");
        Files.copy(is, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

        // 创建 Searcher
        Searcher searcher = Searcher.newWithFileOnly(tempFile.getAbsolutePath());

        // 查询
        String ip = "8.137.89.21";
        String region = searcher.search(ip);
        System.out.println(ip + " => " + region);

        searcher.close();

        System.out.println(Lunar.fromDate(new Date()).toFullString());
        System.out.println(Solar.fromYmd(2016, 1, 1).toFullString());
        System.out.println(HolidayUtil.getHoliday(2020, 5, 2));
    }
}
